﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.Linq;
using System.Linq.Expressions;

namespace MvcApplication1.Controllers
{
    public class SanPhamController : Controller
    {
        //
        // GET: /SanPham/

        public ActionResult ThietBiDiDong()
        {
            DataClassesDataContext dataContext = new DataClassesDataContext();
            var dsSanPham = from sanpham in dataContext.SanPhams where sanpham.MucSp_Id == 1 select sanpham;
            var products = new List<SanPhamModel>();
            
            foreach(SanPham sp in dsSanPham)
            {
                SanPhamModel sanPhamModel = new SanPhamModel();
                sanPhamModel.tenSanPham = sp.TenSp;
                sanPhamModel.giaSanPham = sp.GiaSp.Value;
                sanPhamModel.image = sp.Image;
                sanPhamModel.thongTinChiTiet = sp.ThongTinChiTiet;
                products.Add(sanPhamModel);
            }
            return View(products);
        }

        public ActionResult ThietBiTinHoc()
        {
            DataClassesDataContext dataContext = new DataClassesDataContext();
            var dsSanPham = from sanpham in dataContext.SanPhams where sanpham.MucSp_Id == 2 select sanpham;
            var products = new List<SanPhamModel>();

            foreach (SanPham sp in dsSanPham)
            {
                SanPhamModel sanPhamModel = new SanPhamModel();
                sanPhamModel.tenSanPham = sp.TenSp;
                sanPhamModel.giaSanPham = sp.GiaSp.Value;
                sanPhamModel.image = sp.Image;
                sanPhamModel.thongTinChiTiet = sp.ThongTinChiTiet;
                products.Add(sanPhamModel);
            }
            return View(products);
        }

        public ActionResult TimKiem(int type, string keyword)
        {
            DataClassesDataContext dataContext = new DataClassesDataContext();
            List<SanPhamModel> dsSanPhamModels = new List<SanPhamModel>();
            var dsSanPhamDiDongs = from sp in dataContext.SanPhams where sp.MucSp_Id == type && sp.TenSp.Contains(keyword) select sp;
            foreach (SanPham sanPham in dsSanPhamDiDongs)
            {
                SanPhamModel sanPhamModel = new SanPhamModel();
                sanPhamModel.id = sanPham.Id;
                sanPhamModel.tenSanPham = sanPham.TenSp;
                sanPhamModel.giaSanPham = sanPham.GiaSp.Value;
                sanPhamModel.image = sanPham.Image;
                sanPhamModel.thongTinChiTiet = sanPham.ThongTinChiTiet;
                dsSanPhamModels.Add(sanPhamModel);
            }
            if (dsSanPhamModels.Count == 0)
            {
                ViewBag.EmptyState = "is-empty";
            }
            ViewBag.SanPham = dsSanPhamModels;
            ViewBag.Keyword = keyword;
            return View("TimKiem");
        }
    }
}
